<?php 
namespace Admin\Controller;

class RoleController extends AdminController{
    private $Role = null; //
    //初始化操作
    private $Power = null; //节点表数据库对象
    private $Role_power = null; //角色——权限表数据库对象
    private $User_role = null; //用户--角色表数据库对象
    public function _initialize()
    {
        parent::_initialize();
        $this->Role = D('Role');
        $this->Power = D('Power');
        $this->Role_power = D("role_power");
        $this->User_role = D("user_role");
    }

    public function index()
    {
        $list = $this->Role->select();
        $arr = array();

        //遍历添加上 权限信息
        foreach($list as $v){
            $nodes = $this->Role_power->field('p_id')->where(array('r_id'=>array('eq',$v['id'])))->select();
            $node = array();
            foreach($nodes as $value){
                $node[] = $this->Power->where(array('id'=>array('eq',$value['p_id'])))->getField('name');
            }

            
            $v['none'] = $node;

            $arr[] = $v;
        }



        $this->assign('role_list', $arr);
        $this->display('Role/index');
    }


    public function add()
    {
        $this->display('Role/add');
    }


    public function addo()
    {
        if (IS_POST) {
            if(!$this->Role->create()){
                $this->error($this->Role->getError());
                exit;
            }

            if($this->Role->add() > 0){
                $this->success("添加成功！",U('Role/index'));
            }else{
                $this->error("添加失败！");
            }
            }else{
                $this->error('请求失败!!!!');
        }
    }


    public function edit()
    {
        //查出数据
        $data = array('id'=>I('id'));
        // $id = $data['id'];
        $vo =$this->Role->where($data)->find();
        // dump($vo);
        // exit;
        //向模板分配数据
        $this->assign('vo',$vo);
        // //加载模板
        $this->assign('title','后台用户修改');
        $this->display('Role/edit');
    }



    public function editdo()
    {
        if (IS_POST) {
            // 初始化
            if(!$this->Role->create()){
                $this->error($this->Role->getError());
                exit;
            }

            if ($this->Role->save() >=0) {
                $this->success('修改成功',U('Role/index'));
            }else{
                $this->error('修改失败');
            }





        }else{
            $this->error('请求失败!!');
        }
    }



    public function del()
    {
        // $data = $_GET['id'];
        // $this->ajaxReturn($data);
        // $this->display('Role/index');
        if (IS_GET) {
                

            if ($this->Role->delete(I('id')) > 0){
                $this->success('删除成功',U('Role/index'));
            }else{
                $this->error('删除失败!!!');
            }


        }else{
            $this->error('请求失败!');
        }
    }


    //为角色分配权限
    public function powerlist(){
        //查找该角色信息
        $role = $this->Role->where(array('id'=>array('eq',I('id'))))->find();
        // dump($role);
        // exit;
        //查找所有的节点
        $nodes = $this->Power->select();

        //获取该角色的权限
        $ro_node = $this->Role_power->where(array('r_id'=>array('eq',$role['id'])))->select();
        $ro_nodes = array();
        //遍历重组数组
        foreach($ro_node as $v){
            $ro_nodes[] = $v['p_id'];
        }

        
        

        //向模板分配该用户拥有的权限信息
        $this->assign('ro_nodes',$ro_nodes);
        //向模板分配节点信息
        $this->assign('nodes',$nodes);
        //向模板分配角色信息ec\\
        $this->assign('role',$role);

        //加载模板
        $this->display();


    }


    //为角色添加权限
    public function savenode(){
        // dump($_POST);
        // exit;

        if(empty($_POST['node'])){
            $this->error("必须选择一个节点！");
        }

        $rid = $_POST['rid'];

        //删除该 角色的 所有信息--避免重复添加
        $this->Role_power->where(array('r_id'=>array('eq',$rid)))->delete();

        //循环添加
        foreach($_POST['node'] as $v){
            $data['p_id'] = $v;
            $data['r_id'] = $rid;
            //执行添加
            $this->Role_power->data($data)->add();
        }

        $this->success("添加成功！",U('Role/index'));
    }


}
